package com.feng.controller;

import com.feng.pojo.Course;
import com.feng.service.course.CourseService;
import com.feng.service.course.CourseServiceImpl;
import com.feng.util.TimePlanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addCourse")
public class TeacherServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    	req.setCharacterEncoding("utf-8");
    	
        String courseID = req.getParameter("courseID");
        String courseName = req.getParameter("courseName");
        String teacher = req.getParameter("teacher");
        int point = Integer.parseInt(req.getParameter("point"));
        String time1 = TimePlanUtil.transform(req.getParameter("time1"));
        String time2 = TimePlanUtil.transform(req.getParameter("time2"));
        int limited = Integer.parseInt(req.getParameter("limited"));
        String location = req.getParameter("location");

        Course course = new Course();
        course.setCourseID(courseID);
        course.setCourseName(courseName);
        course.setTeacher(teacher);
        course.setPoint(point);
        course.setTime1(time1);
        course.setTime2(time2);
        course.setLimited(limited);
        course.setLocation(location);

        CourseService courseService = new CourseServiceImpl();
        boolean flag = courseService.addCourse(course);

        if (flag) {
            resp.sendRedirect(req.getContextPath() + "/frame");
        } else {
            resp.sendRedirect(req.getContextPath() + "/frame");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
